 //代下单
 var app_url=location.origin;
$(function(){
    LoadCustomer();//加载
    //复制表头并固定
    notbtn=1;
    chromatable("product_table");
    $(window).resize(function () {
        updateHeader("product_table");
    });

    //筛选
    $("#show_term_btn").on("click", function () {
        $("#common_header").hide();
        $("#product_term_box").show();
    });
    $("#close_term_btn").on("click", function () {
        $("#common_header").show();
        $("#product_term_box").hide();
    });
    //代下单
    $("#product_table").on("click","._place_order",function(){
    	var cid=$(this).attr("data-id");
    	var title=$(this).attr("title");
    	var type=$(this).attr("data-type");
    	var cName=$(this).attr("data-name");
    	var uName=$(this).attr("data-userName");
		
		var han = /^[\u4e00-\u9fa5]+$/;  //验证中文
    	if(type==1){
			if (!han.test(cName))  //不是汉字
				var link_url="/Team/WhiteDias?team=3&cid="+cid+'&userName='+cid;
			else if (!han.test(uName)) //不是汉字
				var link_url="/Team/WhiteDias?team=3&cid="+cid+'&userName='+cid;
			else
				var link_url="/Team/WhiteDias?team=3&cid="+cid+'&userName='+cid;
    		// var link_url="/Team/WhiteDias?team=3&cid="+cid;
    	}else if(type==2){
            if (!han.test(cName))  //不是汉字
                var link_url="/Team/WhiteParcels?team=3&cid="+cid+'&userName='+cName;
            else if (!han.test(uName)) //不是汉字
                var link_url="/Team/WhiteParcels?team=3&cid="+cid+'&userName='+uName;
            else
                var link_url="/Team/WhiteParcels?team=3&cid="+cid+'&userName='+cid;
    	}else if(type==3){
            if (!han.test(cName))  //不是汉字
                var link_url="/Team/Ring?team=3&cid="+cid+'&userName='+cName;
            else if (!han.test(uName)) //不是汉字
                var link_url="/Team/Ring?team=3&cid="+cid+'&userName='+uName;
            else
                var link_url="/Team/Ring?team=3&cid="+cid+'&userName='+cid;
    		//var link_url="/Team/Ring?team=3&cid="+cid;
    	}else if(type==4){
    		var link_url="/Team/Gem?team=3&cid="+cid;
    	}else if(type==5){
    		var link_url="/Team/Emerald?team=3&cid="+cid;
    	}else if(type==6){
    		var link_url="/Team/PrimeGold?team=3&cid="+cid;
    	}else if(type==7){
            if (!han.test(cName))
                var link_url="/Team/InlaySettings?team=3&cid="+cid+'&userName='+cName;
            else if (!han.test(uName))
                var link_url="/Team/InlaySettings?team=3&cid="+cid+'&userName='+uName;
            else
                var link_url="/Team/InlaySettings?team=3&cid="+cid+'&userName='+cid;
        }
    	if(APP){
			var appJson={};
			appJson.title=_("代下单")+_(title);
			appJson.function_menu="ordering_cart";//ordering_cart
			appJson.goods_type=type;
       		appJson.url=app_url+link_url;		           		
       		YiYi_APP.showExistingPage(JSON.stringify(appJson));
		}else{
			location.href=link_url;
		}      
    });
});

//加载
function LoadCustomer() {
	myApp.showIndicator();
    var requestCustomer={};

    requestCustomer.PageSize = 500;
    requestCustomer.PageIndex = 1;
    requestCustomer.SysStatus = 1;

    // if(RoleID != 1){
    //     requestCustomer.SalesUserID = UserID;
    // }
    requestCustomer.UserID=UserID;
    $.ajax({
        url: "/Team/GetCustomers",//提交地址
        type: "POST",//提交方式
        data: JSON.stringify(requestCustomer),
        contentType: "application/json; charset=utf-8",
        success: function (result) {
            BindingData(result, $("#product_table tbody"));//绑定数据
            myApp.hideIndicator();
        }
    });
}

//数据绑定(公用)
function BindingData(Datas, tableTbody) {

    var _result = eval('(' + Datas + ')');//eval()解析单个json对象

    $("#_totalCount").html(_result.TotalCount);//客户总数

    if(_result.msgcode == false){
        //alert(_result["msginfo"]);
        var tr = "";//单行
        var tds = "<td colspan='50' style='text-align:center;color:red'>" + _(_result.msginfo) + "</td>";// 多列合成一列并居中
        tr = "<tr>" + tds + "</tr>";//将多列加到一行
        tableTbody.html(tr);//加入到id为customer的table的tbody中

    }else{

        _result = _result.Data;//object

        var trs = "";//多行数据

        for (var i = 0; i < _result.length; i++) {

            var tds = "";//多列
            var tr = "";//单行
            tds += "<td>" + (i + 1) + "</td>";
            tds += "<td class='user'>" + _result[i].UserName + "</td>";
            tds += "<td class='user'>" + _result[i].NickName + "</td>";
            tds += "<td>" + _result[i].SalesRealName + "</td>"; //SalesUserID
            tds += "<td>" + sysStatusFormat(_result[i].SysStatus) + "</td>";
            if(_result[i].SysStatus == 1){
                if(isDia=="true"){//onclick=Diamonds("+_result[i].CustomerID+",'"+RealNameFormat(_result[i].RealName)+"')
                	tds += "<td class='check_toolbtn'><span class='_place_order' data-type='1' title='代下-裸钻' data-name='"+_result[i].NickName+"' data-userName='"+_result[i].UserName+"'  data-id='" + _result[i].CustomerID + "'>"+_("裸钻")+"</span></td>";
                }
                if(isInlaySettings=="true"){
                    tds += "<td class='check_toolbtn'><span class='_place_order' data-type='7' title='代下-空托' data-name='"+_result[i].NickName+"' data-userName='"+_result[i].UserName+"' data-id='" + _result[i].CustomerID + "'>"+_("空托")+"</span></td>";
                }
                if(isParcel=="true"){
                	tds += "<td class='check_toolbtn'><span class='_place_order' data-type='2' title='代下-统包' data-name='"+_result[i].NickName+"' data-userName='"+_result[i].UserName+"' data-id='" + _result[i].CustomerID + "'>"+_("统包")+"</span></td>";
                }
                if(isRing=="true"){//onclick=Rings("+_result[i].CustomerID+",'"+RealNameFormat(_result[i].RealName)+"') 
                	tds += "<td class='check_toolbtn'><span class='_place_order' data-type='3' title='代下-成品' data-name='"+_result[i].NickName+"' data-userName='"+_result[i].UserName+"' data-id='" + _result[i].CustomerID + "'>"+_("成品")+"</span></td>";
                }
				if(isGem=="true"){//onclick=Gems("+_result[i].CustomerID+",'"+RealNameFormat(_result[i].RealName)+"')
                	tds += "<td class='check_toolbtn'><span class='_place_order' data-type='4' title='代下-彩宝'  data-id='" + _result[i].CustomerID + "'>"+_("彩宝")+"</span></td>";
				}
				if(isEmerald=="true"){
					tds += "<td class='check_toolbtn'><span class='_place_order' data-type='5' title='代下-翡翠'  data-id='" + _result[i].CustomerID + "'>"+_("翡翠")+"</span></td>";
				}
				if(isPrimeGold=="true"){
					tds += "<td class='check_toolbtn'><span class='_place_order' data-type='6' title='代下-素金'  data-id='" + _result[i].CustomerID + "'>"+_("素金")+"</span></td>";
				}
            }else{
                tds += "<td class='check_toolbtn'></td>";
                tds += "<td class='check_toolbtn'></td>";
                tds += "<td class='check_toolbtn'></td>";
                tds += "<td class='check_toolbtn'></td>";
                tds += "<td class='check_toolbtn'></td>";
                tds += "<td class='check_toolbtn'></td>";
            }
            //tr += "<tr>" + tds + "</tr>";//将多列加到一行
            tr += "<tr>" + tds + "</tr>";//将多列加到一行
            trs += tr;//将单行加到多行
        }
        tableTbody.html(trs);//加入到id为customer的table的tbody中
    }
}

//去掉RealName中的空格等多余字符
function RealNameFormat(realname){
    return realname.replace("\"","").replace("\'","").replace(" ","");
}

//客户状态格式化
function sysStatusFormat(sysStatusValue) {
    if (sysStatusValue == -1) {
        return _("已删");
    } else if (sysStatusValue == 0) {
        return _("禁用");
    } else if (sysStatusValue == 2) {
        return _("待审");
    } else if(sysStatusValue == 1){
        return _("正常");
    }
    return sysStatusValue;
}
//获取参数
function getParamer(){

    var requestCustomer={};
    
    //业务员3 采购员4 物流员 5
    if(RoleID== 3)
    {
        // requestCustomer.SalesUserID =UserID;
        requestCustomer.PurchaseUserID = $("#_purchaseUserID").val();

    } else if(RoleID == 4 || RoleID == 5)
    {
        // requestCustomer.SalesUserID =UserID;
    }else
    {
        requestCustomer.SalesUserID = $("#_salesUserID").val();
        requestCustomer.PurchaseUserID = $("#_purchaseUserID").val();
    }
	requestCustomer.UserName =$.trim($("#_userName").val());
	requestCustomer.NickName = $.trim($("#_nickName").val());
	requestCustomer.PageSize = 500; //id=_pageSizes
	requestCustomer.PageIndex = 1;//.page_num是class
    return requestCustomer;
}


//客户搜索
function CustomerSearch(p) {
    var requestCustomer;
    if(p){
        requestCustomer=p;
    }else{
        requestCustomer =getParamer(requestCustomer);
    }
    requestCustomer.UserID=UserID;
    $.ajax({
        url: "/Team/GetCustomers",
        type: "POST",
        data: JSON.stringify(requestCustomer),
        contentType: "application/json",
        success: function (result) {
            BindingData(result, $("#product_table tbody"));
        }
    });
}
